{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "245a954a",
   "metadata": {
    "id": "245a954a"
   },
   "source": [
    "# Alpha Vantage\n",
    "\n",
    ">[Alpha Vantage](https://www.alphavantage.co) Alpha Vantage provides realtime and historical financial market data through a set of powerful and developer-friendly data APIs and spreadsheets. \n",
    "\n",
    "Use the ``AlphaVantageAPIWrapper`` to get currency exchange rates."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "34bb5968",
   "metadata": {
    "id": "34bb5968"
   },
   "outputs": [],
   "source": [
    "import getpass\n",
    "import os\n",
    "\n",
    "os.environ[\"ALPHAVANTAGE_API_KEY\"] = getpass.getpass()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "ac4910f8",
   "metadata": {
    "id": "ac4910f8"
   },
   "outputs": [],
   "source": [
    "from langchain_community.utilities.alpha_vantage import AlphaVantageAPIWrapper"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "84b8f773",
   "metadata": {
    "id": "84b8f773"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'Realtime Currency Exchange Rate': {'1. From_Currency Code': 'USD',\n",
       "  '2. From_Currency Name': 'United States Dollar',\n",
       "  '3. To_Currency Code': 'JPY',\n",
       "  '4. To_Currency Name': 'Japanese Yen',\n",
       "  '5. Exchange Rate': '148.19900000',\n",
       "  '6. Last Refreshed': '2023-11-30 21:43:02',\n",
       "  '7. Time Zone': 'UTC',\n",
       "  '8. Bid Price': '148.19590000',\n",
       "  '9. Ask Price': '148.20420000'}}"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "alpha_vantage = AlphaVantageAPIWrapper()\n",
    "alpha_vantage._get_exchange_rate(\"USD\", \"JPY\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8309d09d",
   "metadata": {},
   "source": [
    "The `_get_time_series_daily` method returns the date, daily open, daily high, daily low, daily close, and daily volume of the global equity specified, covering the 100 latest data points."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "811ae207",
   "metadata": {},
   "outputs": [],
   "source": [
    "alpha_vantage._get_time_series_daily(\"IBM\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b5e46a71",
   "metadata": {},
   "source": [
    "The `_get_time_series_weekly` method returns the last trading day of the week, weekly open, weekly high, weekly low, weekly close, and weekly volume of the global equity specified, covering 20+ years of historical data."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f0dfe35b",
   "metadata": {},
   "outputs": [],
   "source": [
    "alpha_vantage._get_time_series_weekly(\"IBM\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "66cc06a7",
   "metadata": {},
   "source": [
    "The `_get_quote_endpoint` method is a lightweight alternative to the time series APIs and returns the latest price and volume info for the specified symbol."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "98d012ef",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'Global Quote': {'01. symbol': 'IBM',\n",
       "  '02. open': '156.9000',\n",
       "  '03. high': '158.6000',\n",
       "  '04. low': '156.8900',\n",
       "  '05. price': '158.5400',\n",
       "  '06. volume': '6640217',\n",
       "  '07. latest trading day': '2023-11-30',\n",
       "  '08. previous close': '156.4100',\n",
       "  '09. change': '2.1300',\n",
       "  '10. change percent': '1.3618%'}}"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "alpha_vantage._get_quote_endpoint(\"IBM\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3429ce50",
   "metadata": {},
   "source": [
    "The `search_symbol` method returns a list of symbols and the matching company information based on the text entered."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0ccd55b0",
   "metadata": {},
   "outputs": [],
   "source": [
    "alpha_vantage.search_symbols(\"IB\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "96e1fd97",
   "metadata": {},
   "source": [
    "The `_get_market_news_sentiment` method returns live and historical market news sentiment for a given asset."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "42995acb",
   "metadata": {},
   "outputs": [],
   "source": [
    "alpha_vantage._get_market_news_sentiment(\"IBM\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2fdbd888",
   "metadata": {},
   "source": [
    "The `_get_top_gainers_losers` method returns the top 20 gainers, losers and most active stocks in the US market."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "61c3cb1c",
   "metadata": {},
   "outputs": [],
   "source": [
    "alpha_vantage._get_top_gainers_losers()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3d1cf3d8",
   "metadata": {},
   "source": [
    "The `run` method of the wrapper takes the following parameters: from_currency, to_currency. \n",
    "\n",
    "It Gets the currency exchange rates for the given currency pair."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "068991a6",
   "metadata": {
    "id": "068991a6",
    "outputId": "c5cdc6ec-03cf-4084-cc6f-6ae792d91d39"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'1. From_Currency Code': 'USD',\n",
       " '2. From_Currency Name': 'United States Dollar',\n",
       " '3. To_Currency Code': 'JPY',\n",
       " '4. To_Currency Name': 'Japanese Yen',\n",
       " '5. Exchange Rate': '148.19900000',\n",
       " '6. Last Refreshed': '2023-11-30 21:43:02',\n",
       " '7. Time Zone': 'UTC',\n",
       " '8. Bid Price': '148.19590000',\n",
       " '9. Ask Price': '148.20420000'}"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "alpha_vantage.run(\"USD\", \"JPY\")"
   ]
  }
 ],
 "metadata": {
  "colab": {
   "provenance": []
  },
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.1"
  },
  "vscode": {
   "interpreter": {
    "hash": "53f3bc57609c7a84333bb558594977aa5b4026b1d6070b93987956689e367341"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
